﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NutriFast
{
    public partial class UsuarioDel : Form
    {
        public UsuarioDel()
        {
            InitializeComponent();
            
            if (this.dataGridView1.Rows.Count == 0)
                this.button1.Enabled = false;
            else
                this.button1.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show(this, "¿Seguro que desea eliminar?", "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {

                int index = dataGridView1.CurrentRow.Index;
                int id_seleccion = int.Parse("" + dataGridView1.Rows[index].Cells[0].Value);
                if (dbVic.eliminarPaciente(id_seleccion))
                {
                    MessageBox.Show(this, "Paciente Eliminado", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.spVicSerarchPatientTableAdapter.Fill(this.dsVicSearchPatient.spVicSerarchPatient, this.name.Text);
                    if (this.dataGridView1.Rows.Count == 0)
                        this.button1.Enabled = false;
                    else
                        this.button1.Enabled = true;
                
                }
                else
                    MessageBox.Show(this, "Error al eliminar el Paciente", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            

        }

        private void name_TextChanged(object sender, EventArgs e)
        {
            this.spVicSerarchPatientTableAdapter.Fill(this.dsVicSearchPatient.spVicSerarchPatient, this.name.Text);
            if (this.dataGridView1.Rows.Count == 0)
                this.button1.Enabled = false;
            else
                this.button1.Enabled = true;


            dataGridView1_SelectionChanged(null, null);


        }

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {

            dataGridView1_SelectionChanged(null, null);

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                int id_seleccion = int.Parse("" + dataGridView1.Rows[index].Cells[0].Value);
                string filename = "../../Fotos/" + id_seleccion + ".jpg";
                pictureBox1.Image = new Bitmap(filename);

            }
            catch (Exception)
            {
                pictureBox1.Image = NutriFast.Properties.Resources.defaultUser;
            }
        }

        private void UsuarioDel_Load(object sender, EventArgs e)
        {
            name_TextChanged(null, null);
        }
    }
}
